﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using Yugi.Client.DataModel;
using Yugi.Client;
using Yugi_Gui;

namespace Yugi.Client.Control
{
    /// <summary>
    /// Interaction logic for YCardList.xaml
    /// </summary>
    public partial class YCardList : UserControl, IInvoke
    {
        PlayerModel model;
        //bool da_len_bai_summon;
        internal List<HandImage> Data = new List<HandImage>();
        public YCardList()
        {
            this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(YCardList_DataContextChanged);
            locklick = 0;
        }

        void YCardList_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!(e.NewValue is PlayerModel)) return;
            model = (PlayerModel)e.NewValue;

            model.Presenter = this;
            this.DataContext = (PlayerModel)e.NewValue;
            model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(p_PropertyChanged);
        }

        void p_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "YStateNow")
            {
                switch (model.YStateNow)
                {
                    case EnumTable.DrawPhase:

                        break;
                    case EnumTable.MainPhase1:
                        //da_len_bai_summon = false;
                        //Block.Block.Allow_NormalSummon = true;
                        //Block.Block.Allow_SpecialSummon = true;
                        break;

                }
            }
            else if (e.PropertyName == "YListCard")
            {
                LoadCard();
            }

        }

        /// <summary>
        /// Load Card
        /// </summary>
        private void LoadCard()
        {
            int dem = 0;
            foreach (var item in model.YListCard)
            {
                HandImage img = new HandImage();

                if (item.YCard != null)
                {
                    img.MouseEnter += new MouseEventHandler(img_MouseEnter);
                    img.MouseRightButtonDown += new MouseButtonEventHandler(img_MouseRightButtonDown);
                    img.MouseLeave += new MouseEventHandler(img_MouseLeave);
                    img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                    img.DataContext = item;
                    img.MImage.Source = item.YCard.displayImage;
                    img.Margin = new Thickness(-20, 0, 0, 0);
                }
                else
                {
                    img.DataContext = dem++;
                    img.MImage.Source = new BitmapImage(new Uri("pack://application:,,,/Image/card.png"));
                    img.Margin = new Thickness(-10, 0, 0, 5);
                }
                Data.Add(img);
                this.stack.Children.Add(img);
            }
        }


        //private bool InitMenuSpecialSummon(YBaseCard card, HandImage img)
        //{
        //    YMonsterCard m_card = card as YMonsterCard;
        //    if (m_card.Level > Block.Block.SpecialSummonStar)
        //    {
        //        img.ContextMenu = new ContextMenu();
        //        img.ContextMenu.Opacity = 0.8;
        //        img.ContextMenu.BorderBrush = (Brush)new BrushConverter().ConvertFrom("#FFFFEB00");
        //        img.ContextMenu.FontWeight = (FontWeight)new FontWeightConverter().ConvertFrom("Bold");
        //        img.ContextMenu.FontFamily = (FontFamily)new FontFamilyConverter().ConvertFrom("Comic Sans MS");
        //        MenuItem i = new MenuItem();
        //        i.Header = "Summon Dac Biet Tan Cong";
        //        i.DataContext = img.DataContext;
        //        i.Click += new RoutedEventHandler(Popup_Click);
        //        img.ContextMenu.Items.Add(i);
        //        i = new MenuItem();
        //        i.Header = "Summon Dac Biet Phong Thu";
        //        i.DataContext = img.DataContext;
        //        i.Click += new RoutedEventHandler(Popup_Click);
        //        img.ContextMenu.Items.Add(i);
        //        return true;
        //    }
        //    return false;
        //}
        //private void InitMenuChonBai()
        //{
        //    foreach (var item in stack.Children)
        //    {
        //        HandImage img = item as HandImage;
        //        CardModel model = img.DataContext as CardModel;
        //        switch(model.YCard.GetType().ToString())
        //        {
        //            case "Yugi.Client.Card.YMonsterCard":
        //                // Init Menu card > 4 sao
        //                if (InitMenuSpecialSummon(model.YCard, img)) continue;
        //                // Init Menu card < 4sao
        //                img.ContextMenu = new ContextMenu();
        //                img.ContextMenu.Opacity = 0.8;
        //                img.ContextMenu.BorderBrush = (Brush)new BrushConverter().ConvertFrom("#FFFFEB00");
        //                img.ContextMenu.FontWeight = (FontWeight)new FontWeightConverter().ConvertFrom("Bold");
        //                img.ContextMenu.FontFamily = (FontFamily)new FontFamilyConverter().ConvertFrom("Comic Sans MS");
        //                MenuItem i = new MenuItem();
        //                i.Header = "Summon";
        //                i.DataContext = img.DataContext;
        //                i.Click += new RoutedEventHandler(Popup_Click);
        //                img.ContextMenu.Items.Add(i);
        //                i = new MenuItem();
        //                i.Header = "Set";
        //                i.DataContext = img.DataContext;
        //                i.Click += new RoutedEventHandler(Popup_Click);
        //                img.ContextMenu.Items.Add(i);
        //            break;
        //            case "Yugi.Client.Card.YSpellCard":
        //                img.ContextMenu = new ContextMenu();
        //                img.ContextMenu.Opacity = 0.8;
        //                img.ContextMenu.BorderBrush = (Brush)new BrushConverter().ConvertFrom("#FFFFEB00");
        //                img.ContextMenu.FontWeight = (FontWeight)new FontWeightConverter().ConvertFrom("Bold");
        //                img.ContextMenu.FontFamily = (FontFamily)new FontFamilyConverter().ConvertFrom("Comic Sans MS");
        //                i = new MenuItem();
        //                i.Header = "Set Spell";
        //                i.DataContext = img.DataContext;
        //                i.Click += new RoutedEventHandler(Popup_Click);
        //                img.ContextMenu.Items.Add(i);
        //            break;
        //            case "Yugi.Client.Card.YTrapCard":
        //                img.ContextMenu = new ContextMenu();
        //                img.ContextMenu.Opacity = 0.8;
        //                img.ContextMenu.BorderBrush = (Brush)new BrushConverter().ConvertFrom("#FFFFEB00");
        //                img.ContextMenu.FontWeight = (FontWeight)new FontWeightConverter().ConvertFrom("Bold");
        //                img.ContextMenu.FontFamily = (FontFamily)new FontFamilyConverter().ConvertFrom("Comic Sans MS");
        //                i = new MenuItem();
        //                i.Header = "Set Trap";
        //                i.DataContext = img.DataContext;
        //                i.Click += new RoutedEventHandler(Popup_Click);
        //                img.ContextMenu.Items.Add(i);
        //            break;
        //        }
        //    }
        //}
        //private void DelMenuChonBai()
        //{
        //    foreach (var item in stack.Children)
        //    {
        //        HandImage img = item as HandImage;
        //        img.ContextMenu = null;
        //    }
        //}
        private HandImage Find(int id, out int index)
        {
            index = 0;
            foreach (var item in stack.Children)
            {
                HandImage img = item as HandImage;
                if (((CardModel)img.DataContext).YCard.ID == id)
                {
                    return img;
                }
                index++;
            }
            return null;
        }

        internal HandImage FindControl(int id)
        {
            foreach (var item in stack.Children)
            {
                HandImage img = item as HandImage;
                if (((CardModel)img.DataContext).YCard.ID == id)
                {
                    return img;
                }
            }
            return null;
        }

        void Popup_Click(object sender, RoutedEventArgs e)//click vao contextmenu
        {
            MenuItem item = sender as MenuItem;
            YBaseCard card = ((Yugi.Client.CardModel)item.DataContext).YCard;
            switch (item.Header.ToString())
            {
                case "Summon":
                    if (card.CardType == YType.Monster && Block.Block.Allow_NormalSummon) //da_len_bai_summon == false)
                        model.ReciveMessageFromView(ClientMSG.ChonBaiXong, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
                    break;
                case "Set":
                    if (card.CardType == YType.Monster && Block.Block.Allow_NormalSummon) //da_len_bai_summon == false)
                        model.ReciveMessageFromView(ClientMSG.ChonBaiXong, card, Common.YMSG.CARD_TRANGTHAI_NAM_UP);
                    break;
                case "Set Spell":
                    if (card.CardType == YType.Spell)
                        model.ReciveMessageFromView(ClientMSG.LenBaiSpell, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                    break;
                case "Set Trap":
                    if (card.CardType == YType.Trap)
                        model.ReciveMessageFromView(ClientMSG.LenBaiTrap, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                    break;
                case "Summon Dac Biet Tan Cong":
                    if (card.CardType == YType.Monster && Block.Block.Allow_SpecialSummon) // da_len_bai_summon == false)
                        model.ReciveMessageFromView(ClientMSG.LenBaiDacBiet, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
                    break;
                case "Summon Dac Biet Phong Thu":
                    if (card.CardType == YType.Monster && Block.Block.Allow_SpecialSummon) //da_len_bai_summon == false)
                        model.ReciveMessageFromView(ClientMSG.LenBaiDacBiet, card, Common.YMSG.CARD_TRANGTHAI_NAM_UP);
                    break;
            }
        }
        //void Popup_Click_002(object sender)
        //{
        //    HandImage image = sender as HandImage;
        //    YBaseCard card = ((Yugi.Client.CardModel)image.DataContext).YCard;
        //    if (card.CardType == YType.Monster)
        //    {
        //        YMonsterCard m_card = (YMonsterCard)card;
        //        if (m_card.Level > Block.Block.SpecialSummonStar)
        //        {
        //            model.ReciveMessageFromView(ClientMSG.LenBaiDacBiet, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
        //        }
        //        else if (Block.Block.Allow_NormalSummon)
        //        {
        //            model.ReciveMessageFromView(ClientMSG.ChonBaiXong, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
        //        }

        //    }
        //    else if (card.CardType == YType.Spell)
        //    {
        //        model.ReciveMessageFromView(ClientMSG.LenBaiSpell, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
        //    }
        //    else if (card.CardType == YType.Trap)
        //    {
        //        model.ReciveMessageFromView(ClientMSG.LenBaiTrap, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
        //    }
        //}
        void img_MouseEnter(object sender, MouseEventArgs e)
        {
            if (chonbai == 1)
            {
                HandImage image = sender as HandImage;
                YBaseCard card = ((Yugi.Client.CardModel)image.DataContext).YCard;
                if (card.CardType == YType.Monster)
                {
                    YMonsterCard m_card = (YMonsterCard)card;
                    if (m_card.Level > Block.Block.SpecialSummonStar)
                    {
                        ability("summon", image);
                    }
                    else if (Block.Block.Allow_NormalSummon)
                    {
                        ability("summon", image);
                    }
                }
                else if (card.CardType == YType.Spell || card.CardType == YType.Trap)
                {
                    ability("set", image);
                }
            }
            HandImage img = sender as HandImage;
            this.model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.PrintCard, ((CardModel)img.DataContext).YCard);
        }
        void img_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (chonbai == 1)
            {
                HandImage image = sender as HandImage;
                YBaseCard card = ((Yugi.Client.CardModel)image.DataContext).YCard;
                if (card.CardType == YType.Monster)
                {
                    YMonsterCard m_card = (YMonsterCard)card;
                    if (m_card.Level > Block.Block.SpecialSummonStar)
                    {
                        ability("set", image);
                    }
                    else if (Block.Block.Allow_NormalSummon)
                    {
                        ability("set", image);
                    }

                }
            }
        }

        void img_MouseLeave(object sender, MouseEventArgs e)
        {
            HandImage image = sender as HandImage;
            image.image_ability.Visibility = Visibility.Hidden;
        }
        public void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (chonbai == 1 && locklick ==0)
            {
                HandImage image = sender as HandImage;
                YBaseCard card = ((Yugi.Client.CardModel)image.DataContext).YCard;
                lenbai(card);
            }
        }
        void lenbai(YBaseCard card)
        {
            if (act == "summon")
            {
                YMonsterCard m_card = (YMonsterCard)card;
                if (card.CardType == YType.Monster && Block.Block.Allow_NormalSummon&&m_card.Level<=Block.Block.SpecialSummonStar) //da_len_bai_summon == false)
                    model.ReciveMessageFromView(ClientMSG.ChonBaiXong, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
                else if (card.CardType == YType.Monster && Block.Block.Allow_SpecialSummon) // da_len_bai_summon == false)
                    model.ReciveMessageFromView(ClientMSG.LenBaiDacBiet, card, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
            }
            else if (act == "set")
            {
                if (card.CardType == YType.Monster && Block.Block.Allow_NormalSummon) //da_len_bai_summon == false)
                    model.ReciveMessageFromView(ClientMSG.ChonBaiXong, card, Common.YMSG.CARD_TRANGTHAI_NAM_UP);
                else if (card.CardType == YType.Spell)
                    model.ReciveMessageFromView(ClientMSG.LenBaiSpell, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                else if (card.CardType == YType.Trap)
                    model.ReciveMessageFromView(ClientMSG.LenBaiTrap, card, Common.YMSG.CARD_TRANGTHAI_DUNG_UP);
                else if (card.CardType == YType.Monster && Block.Block.Allow_SpecialSummon) //da_len_bai_summon == false)
                    model.ReciveMessageFromView(ClientMSG.LenBaiDacBiet, card, Common.YMSG.CARD_TRANGTHAI_NAM_UP);
            }
        }
        string act;
        void ability(string arg1, HandImage image)
        {
            act = arg1;
            string s = "/Yugi_Gui;component/Image/" + act + ".png";
            image.image_ability.Source = new BitmapImage(new Uri(s, UriKind.Relative));
            image.image_ability.Visibility = Visibility.Visible;
        }
        void Process_Request(RequestAPI.ReqestItem item)
        {
            if (item.Type == Common.YMSG.REQUEST_CHONBAI_NGUOICHOI_TAY)
            {
                choice_nguoichoi.Data = this.Data;
                choice_nguoichoi.Count = item.Number;
                choice_nguoichoi.SelectionType = YType.Monster;
                choice_nguoichoi.OnError += new Action<object, string>(choice_nguoichoi_OnError);
                choice_nguoichoi.OnChoiceComplete += new EventHandler(choice_nguoichoi_OnChoiceComplete);
                is_show_dialog_request = true;
                choice_nguoichoi.Begin();
                if (is_show_dialog_request)
                {
                    Yugi_Gui.Linker.ShowDialogBox("Chọn " + item.Number + " lá bài trên tay");
                }
            }
            else if (item.Type == Common.YMSG.REQUEST_CHONBAI_DOITHU_TAY)
            {
                choice_doithu.Data = this.Data;
                choice_doithu.Count = item.Number;
                choice_doithu.OnError += new Action<object, string>(choice_doithu_OnError);
                choice_doithu.OnChoiceComplete += new EventHandler(choice_doithu_OnChoiceComplete);
                is_show_dialog_request = true;
                choice_doithu.Begin();
                if (is_show_dialog_request)
                {
                    Yugi_Gui.Linker.ShowDialogBox("Chọn " + item.Number + " lá bài trên tay đối thủ");
                }
            }

        }

        //---------------------------Doi Thu Handler-------------------------------
        void choice_doithu_OnChoiceComplete(object sender, EventArgs e)
        {
            //Init Answer
            RequestAPI.ReqestItem item = RequestAPI.Get();
            List<object> ans = new List<object>();
            ans.Add(item.ID);
            ans.Add(item.Number);
            foreach (var i in choice_doithu.Choices)
            {
                ans.Add(i);
            }
            // Send Answer   
            RequestAPI.SendResponse(ans.ToArray());

            // Delete Delegate
            choice_doithu.OnChoiceComplete -= new EventHandler(choice_doithu_OnChoiceComplete);
            choice_doithu.OnError -= new Action<object, string>(choice_doithu_OnError);
        }

        void choice_doithu_OnError(object arg1, string arg2)
        {
            is_show_dialog_request = false;

            RequestAPI.ReqestItem item = RequestAPI.Get();
            RequestAPI.SendResponse(item.ID, item.Number, -1);

            // Delete Delegate
            choice_doithu.OnChoiceComplete -= new EventHandler(choice_doithu_OnChoiceComplete);
            choice_doithu.OnError -= new Action<object, string>(choice_doithu_OnError);
        }
        //---------------------------Nguoi Choi Handler-------------------------------
        void choice_nguoichoi_OnChoiceComplete(object sender, EventArgs e)
        {
            //Init Answer
            RequestAPI.ReqestItem item = RequestAPI.Get();
            List<object> ans = new List<object>();
            ans.Add(item.ID);
            ans.Add(item.Number);
            foreach (var i in choice_nguoichoi.Choices)
            {
                ans.Add(i.ID);
            }
            // Send Answer   
            RequestAPI.SendResponse(ans.ToArray());

            // Delete Delegate
            choice_nguoichoi.OnChoiceComplete -= new EventHandler(choice_nguoichoi_OnChoiceComplete);
            choice_nguoichoi.OnError -= new Action<object, string>(choice_nguoichoi_OnError);
        }

        void choice_nguoichoi_OnError(object arg1, string arg2)
        {
            is_show_dialog_request = false;

            RequestAPI.ReqestItem item = RequestAPI.Get();
            RequestAPI.SendResponse(item.ID, item.Number, -1);

            // Delete Delegate
            choice_nguoichoi.OnChoiceComplete -= new EventHandler(choice_nguoichoi_OnChoiceComplete);
            choice_nguoichoi.OnError -= new Action<object, string>(choice_nguoichoi_OnError);
        }
        //----------------------------------------------------------------------------
        CardChoiceHandDoiThu choice_doithu = new CardChoiceHandDoiThu();
        CardChoiceHandNguoiChoi choice_nguoichoi = new CardChoiceHandNguoiChoi();
        bool is_show_dialog_request;
        private int chonbai;
        #region IInvoke Members

        public void Do(ClientMSG msg, params object[] obj)
        {
            switch (msg)
            {
                case ClientMSG.RutBai:
                    YBaseCard item = obj[0] as YBaseCard;
                    HandImage img = new HandImage();
                    img.MouseEnter += new MouseEventHandler(img_MouseEnter);
                    CardModel cmodel = new CardModel(item, model.YSupervisorModel);
                    model.YListCard.Add(cmodel);
                    img.DataContext = cmodel;
                    img.MImage.Source = item.displayImage;
                    img.Margin = new Thickness(-20, 0, 0, 0);
                    img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                    img.MouseRightButtonDown += new MouseButtonEventHandler(img_MouseRightButtonDown);
                    img.MouseLeave += new MouseEventHandler(img_MouseLeave);
                    Data.Add(img);
                    this.stack.Children.Add(img);
                    break;
                //------------------- Len Bai Quai Vat------------------------------
                case ClientMSG.ChonBai:
                    //InitMenuChonBai();
                    chonbai = 1;
                    break;
                case ClientMSG.LenBaiHopLe:
                    int index;
                    img = Find((int)obj[0], out index);
                    //Xoa
                    Data.RemoveAt(index);
                    this.stack.Children.Remove(img);
                    this.model.YListCard.Remove(img.DataContext as CardModel);

                    // Kiem Tra La Bai Vua Len La Bai Summon Hay Khong
                    if (((CardModel)img.DataContext).YCard.CardType == YType.Monster)
                    {
                        //da_len_bai_summon = true;
                        Block.Block.Allow_NormalSummon = false;
                        Block.Block.Allow_SpecialSummon = false;
                    }
                    //Xoa len bai dac biet neu co
                    model.YSupervisorModel.TableGame.Presenter.Do(ClientMSG.XoaLenBaiDacBiet);

                    break;
                case ClientMSG.KetThucMF:
                    //DelMenuChonBai();
                    chonbai = 0;
                    break;
                //---------------------Len Bai Dac Biet-------------------------//
                case ClientMSG.LenBaiDacBietHopLe:
                    img = Find((int)obj[0], out index);
                    //Xoa
                    Data.RemoveAt(index);
                    this.stack.Children.Remove(img);
                    this.model.YListCard.Remove(img.DataContext as CardModel);
                    // Kiem Tra La Bai Vua Len La Bai Summon Hay Khong
                    if (((CardModel)img.DataContext).YCard.CardType == YType.Monster)
                    {
                        //da_len_bai_summon = true;
                        Block.Block.Allow_NormalSummon = false;
                        Block.Block.Allow_SpecialSummon = false;
                    }
                    //Xoa len bai dac biet neu co
                    model.YSupervisorModel.TableGame.Presenter.Do(ClientMSG.XoaLenBaiDacBiet);
                    break;
                //------------------------Bai Doi Thu Tren Tay--------------------------//
                case ClientMSG.ThemBaiDoiThuTay:
                    img = new HandImage();
                    img.DataContext = this.stack.Children.Count;
                    img.MImage.Source = new BitmapImage(new Uri("pack://application:,,,/COntrol/BackMaterial.jpg"));
                    img.Margin = new Thickness(-20, 0, 0, 5);
                    img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                    Data.Add(img);
                    this.stack.Children.Add(img);
                    break;
                case ClientMSG.XoaBaiDoiThuTay:
                    if (this.stack.Children.Count != 0)
                    {
                        Data.RemoveAt(0);
                        this.stack.Children.RemoveAt(0);
                    }
                    break;
                //--------------------------Request------------------
                case ClientMSG.Request:
                    RequestAPI.ReqestItem req_item = RequestAPI.Get();
                    Process_Request(req_item);
                    break;
                //---------------------------Len Bai Spell-------------//
                case ClientMSG.LenBaiSpellHopLe:

                    img = Find((int)obj[0], out index);
                    //Xoa
                    Data.RemoveAt(index);
                    this.stack.Children.Remove(img);
                    this.model.YListCard.Remove(img.DataContext as CardModel);

                    break;
                //---------------------------Len Bai Trap-------------//
                case ClientMSG.LenBaiTrapHopLe:

                    img = Find((int)obj[0], out index);
                    //Xoa
                    Data.RemoveAt(index);
                    this.stack.Children.Remove(img);
                    this.model.YListCard.Remove(img.DataContext as CardModel);

                    break;
                //---------------------------API-----------------------//
                case ClientMSG.Api_DrawCard:
                    // Them Bai Nguoi Choi
                    if ((int)obj[0] == this.model.YSupervisorModel.Nguoi_Choi.YPlayer.ID)
                    {
                        item = obj[1] as YBaseCard;
                        img = new HandImage();
                        img.MouseEnter += new MouseEventHandler(img_MouseEnter);
                        cmodel = new CardModel(item, model.YSupervisorModel);
                        model.YListCard.Add(cmodel);
                        img.DataContext = cmodel;
                        img.MImage.Source = item.displayImage;
                        img.Margin = new Thickness(-20, 0, 0, 0);
                        img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                        Data.Add(img);
                        this.stack.Children.Add(img);
                    }
                    // Them Bai Doi Thu
                    else
                    {
                        img = new HandImage();
                        img.DataContext = this.stack.Children.Count;
                        img.MImage.Source = new BitmapImage(new Uri("pack://application:,,,/COntrol/BackMaterial.jpg"));
                        img.Margin = new Thickness(-20, 0, 0, 5);
                        img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
                        Data.Add(img);
                        this.stack.Children.Add(img);
                    }
                    break;
                case ClientMSG.XoaBaiTay:
                    img = Find((int)obj[0], out index);
                    Data.RemoveAt(index);
                    this.stack.Children.Remove(img);
                    this.model.YListCard.Remove(img.DataContext as CardModel);
                    break;
                case ClientMSG.Block_Wait_Active_Begin:
                    Linker.message_box.ShowWait();
                    break;
                case ClientMSG.Block_Wait_Active_End:
                    Linker.message_box.CloseWait();
                    break;
                //---------------------------Reset---------------------//
                case ClientMSG.Reset:
                    Reset();
                    break;
            }
            //---Forward 
            model.Deck_Presenter.Do(msg, obj);
        }


        public void Refresh()
        {
        }
        #endregion

        public void Reset()
        {
            locklick = 0;
            this.Data.Clear();
            this.stack.Children.Clear();
            model.Reset();
        }

        internal void lock_click()
        {
            locklick = 1;
        }

        internal void unlock_click()
        {
            locklick = 0;
        }

        public int locklick { get; set; }
    }
}